package me.mononin.factory.persistence;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.raizlabs.android.dbflow.sql.language.SQLite;

import me.mononin.factory.Factory;
import me.mononin.factory.model.api.account.AccountRspModel;
import me.mononin.factory.model.db.User;
import me.mononin.factory.model.db.User_Table;

/**
 * @author Yaning
 * @description
 * @date 18-4-21 下午6:27
 */
public class Account {

    private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
    private static final String KEY_IS_BIND = "KEY_IS_BIND";
    private static final String KEY_TOKEN = "KEY_TOKEN";
    private static final String KEY_USER_ID = "KEY_USER_ID";
    private static final String KEY_ACCOUNT = "KEY_ACCOUNT";

    private static String pushId;
    private static boolean isBind;
    // 登录状态的Token，用来接口请求
    private static String token;
    // 登录的用户ID
    private static String userId;
    // 登录的账户
    private static String account;

    public static String getPushId() {
        return pushId;
    }

    public static void setPushId(String pushId) {
        Account.pushId = pushId;
        Account.save(Factory.app());
    }


    /**
     * 存储数据到XML文件，持久化
     */
    private static void save(Context context) {
        // 获取数据持久化的SP
        SharedPreferences sp = context.getSharedPreferences(Account.class.getName(),
                Context.MODE_PRIVATE);
        // 存储数据
        sp.edit()
                .putString(KEY_PUSH_ID, pushId)
                .putBoolean(KEY_IS_BIND,isBind)
                .putString(KEY_TOKEN, token)
                .putString(KEY_USER_ID, userId)
                .putString(KEY_ACCOUNT, account)
                .apply();
    }



    /**
     * 进行数据加载
     */
    public static void load(Context context) {
        SharedPreferences sp = context.getSharedPreferences(Account.class.getName(),
                Context.MODE_PRIVATE);
        pushId = sp.getString(KEY_PUSH_ID, "");
        isBind = sp.getBoolean(KEY_IS_BIND,false);
        token = sp.getString(KEY_TOKEN, "");
        userId = sp.getString(KEY_USER_ID, "");
        account = sp.getString(KEY_ACCOUNT, "");
    }

    /**
     * 是否已经登录
     * @return isLogin
     */
    public static boolean isLogin(){
        return !TextUtils.isEmpty(userId)
                &&!TextUtils.isEmpty(token);
    }

    /**
     * 是否绑定设备
     * @return isBind
     */
    public static boolean isBind(){
        return isBind;
    }

    /**
     * 设置设备是否绑定服务器
     * @param b 是否绑定服务器True or False
     */
    public static void setIsBind(boolean b){
        Account.isBind = b;
        Account.save(Factory.app());
    }

    /**
     * 保存用户信息到本地XML文件中
     * @param model AccountRspModel
     */
    public static void login(AccountRspModel model){
        userId = model.getCard().getId();
        account = model.getAccount();
        token = model.getToken();
        save(Factory.app());
    }

    /**
     * 快速获取当前用户信息
     * @return user
     */
    public static User getUser(){
        return TextUtils.isEmpty(userId)?new User(): SQLite.select()
                .from(User.class)
                .where(User_Table.id.eq(userId))
                .querySingle();
    }


    /**
     * 是否已经完善了用户信息
     *
     * @return True 是完成了
     */
    public static boolean isComplete() {
        // 首先保证登录成功
        if (isLogin()) {
            User self = getUser();
            return !TextUtils.isEmpty(self.getDesc())
                    && !TextUtils.isEmpty(self.getPortrait())
                    && self.getSex() != 0;
        }
        // 未登录返回信息不完全
        return false;
    }

    /**
     * 返回用户Id
     *
     * @return 用户Id
     */
    public static String getUserId() {
        return getUser().getId();
    }

    /**
     * 返回用户账号
     *
     * @return 用户账号
     */
    public static String getAccount() {
        return getUser().getPhone();
    }

    /**
     * 获取当前登录的Token
     *
     * @return Token
     */
    public static String getToken() {
        return token;
    }

}
